import Vue from 'vue'
import store from '../../store'
import XThirdLogin from '../../x/XThirdLogin'
import XPay from '../../x/XPay'
import XCookie from "../../x/XCookie";


export default {
    redirectedURLFn (to, next) {
        if (to.path.includes('/store/home')) {
            let query = to.query
            let queryString = '';
            let path = '';
            for (let value in query) {
                queryString += Vue.prototype.hrefHasQuery(queryString) + value + '=' + query[value]
            }
            if (to.query && to.query.menuType) {
                switch (Number(to.query.menuType)) {
                    //1纯媒体 2活动 3商品 4买家秀 5大师
                    case 1: path = '/store/panorama/' + Vue.prototype.random4Num(); break;
                    case 2: path = '/store/promotion/' + Vue.prototype.random4Num(); break;
                    case 3: path = '/store/goods/' + Vue.prototype.random4Num(); break;
                    case 4: path = '/store/buyShow/' + Vue.prototype.random4Num(); break;
                    case 5: path = '/store/master/' + Vue.prototype.random4Num(); break;
                    case 6: path = '/store/playDirect/' + Vue.prototype.random4Num(); break;
                    default: path = to.path.replace('/store/home', '/store/');
                }
                window.location.href = path + queryString;
            } else {
                window.location.href = to.path.replace('/store/home', '/store/') + queryString;
            }
            // window.location.href = to.path.replace('/store/home','/store/') + queryString;
        }
        return false
    },
    isBreak (to, next) {
        if (to.path == "/favicon.ico") {
            return true
        }
        return false
    },
    isPreview (to, next) {
        if (to.path == "") {
            return true;
        }
        return false;
    },
    isDebug (to, next) {
        if (to.path == '/vue/xdebug') {
            return true;
        }
        return false;
    },
    toLogin (to, next) {
        if (to.path.includes("/to/login")) {
            return true;
        }
        return false
    },

    /**
     * 拦截第三方认证回调
     * @param to
     * @param next
     * @returns {boolean}
     */
    toThirdAuth (to, next) {
        let isFinish = false;
        let tempI = 0;
        function callback () {
            isFinish = true
        }
        switch (to.path) {
            case "/wechat_auth":
                XThirdLogin.auth4Wechat(to.query, callback)
                break;
            case "/wechat/prepay":
                XThirdLogin.prepay4Wechat(to.query, callback)
                break;
            case "/qq_auth":
                XThirdLogin.auth4QQ(to.query, callback)
                break;
            case "/alipay_auth":
                XThirdLogin.auth4Alipay(to.query, callback)
                break;
            default:
                return false
        }
        let timer = setInterval(() => {
            if (isFinish && tempI < 50) {
                tempI++;
                let nextPage = window.localStorage.getItem(Vue.prototype.NEXTPAGE) || '';
                XCookie.doAlert("nextPage : " + nextPage);
                XCookie.doAlert("to.path : " + to.path);
                XCookie.doAlert("!to.path.includes(nextPage) : " + !to.path.includes(nextPage));
                if (!to.path.includes(nextPage)) {
                    if (nextPage) {
                        window.location.href = nextPage;
                    } else {
                        Vue.$router.push({ path: '/home' })
                    }
                }
                clearInterval(timer)
            }
        }, 100)
        return true

    },
    /**
     * 拦截支付返回回调
     * @param to
     * @param next
     * @returns {boolean}
     */
    payReturn (to, next) {
        switch (to.path) {
            case "/open/zero/payResult":
                XPay.payReturn4Zero(to.query)
                break;
            case "/pay/ali/return.htm":
                XPay.payReturn4Alipay(to.query)
                break;
            case "/pay/wechat/return":
                XPay.payReturn4Wechat(to.query)
                break;
            default:
                return false
        }
        return true
    },
    addTtn (to, from) {
        if (to.query && to.query.ttn) {
            store.state.ttn = to.query.ttn;
        } else {
            store.state.ttn = 6
        }
        Vue.prototype.ttn = store.state.ttn;
    },
    isPreviewController (to, next) {
        if (to.query && to.query.previewController && to.query.ttn == 1) {
            return true
        }
        return false
    }
}
